package org.example.traing.util;


import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

        @ExceptionHandler(HttpMessageNotReadableException.class)
        @ResponseStatus(code = HttpStatus.BAD_REQUEST)
        @ResponseBody
        public ResponseEntity<Object> handlerMissingServletRequestParameterException(HttpMessageNotReadableException e) {
                Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

                /*e.printStackTrace();*/

                logger.error(e.getMessage());
                Map<String, Object> response = new HashMap<>();
                response.put("msg", "缺少json参数");
                return ResponseEntity.ok(response);
        }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ResponseEntity<Object> handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

        /*e.printStackTrace();*/

        logger.error(e.getMessage());
        Map<String, Object> response = new HashMap<>();
        response.put("msg", e.getMessage());
        return ResponseEntity.ok(response);
    }





}



